/*
 * @Author: RidingWind
 * @Date: 2018-02-09 13:55:37
 * @Last Modified by: 大招科技 - RidingWind
 * @Last Modified time: 2018-03-01 13:36:35
 */
const gulp = require('gulp');
const plumber = require('gulp-plumber');
const rename = require('gulp-rename');
const sourcemaps = require('gulp-sourcemaps');
const rev = require('gulp-rev');
const revCollector = require('gulp-rev-collector');
const clean = require('gulp-clean');

// css
const postcss = require('gulp-postcss');
const postcssImport = require('postcss-import');
const autoprefixer = require('autoprefixer');
const postcssCustomProperties = require('postcss-custom-properties');
const postcssApply = require('postcss-apply');
const postcssCalc = require('postcss-calc');
const postcssRgba = require('postcss-color-rgba-fallback');
const postcssPseudoelements = require('postcss-pseudoelements');
const pixrem = require('pixrem');
const cleanCSS = require('gulp-clean-css'); // 压缩css

// js
const eslint = require('gulp-eslint');
const rollup = require('gulp-rollup');
const commonjs = require('rollup-plugin-commonjs');
const nodeResolve = require('rollup-plugin-node-resolve');
const buble = require('rollup-plugin-buble');
const uglify = require('gulp-uglify'); // 压缩js

// 压缩图片
const imagemin = require('gulp-imagemin');

// 压缩hml
const htmlmin = require('gulp-htmlmin');

// dev server
const browserSync = require('browser-sync').create();

// 拷贝第三方文件到dist目录；
gulp.task('lib', () => {
  gulp.src([
    './src/common/js/es5-sham.min.js',
    './src/common/js/es5-shim.min.js',
    './src/common/js/html5shiv.min.js',
    './node_modules/bootstrap/dist/css/bootstrap.min.css',
    './node_modules/bootstrap/dist/js/bootstrap.min.js',
    './node_modules/jquery/dist/jquery.min.js',
    './node_modules/isotope-layout/dist/isotope.pkgd.min.js',
    './node_modules/imagesloaded/imagesloaded.pkgd.min.js',
  ])
    .pipe(gulp.dest('./dist/lib'));
});

gulp.task('image', () => {
  gulp.src('./src/**/*.{jpg,png}')
    .pipe(plumber())
    .pipe(rename({ dirname: '' }))
    .pipe(imagemin())
    .pipe(gulp.dest('./dist/images'))
    .pipe(browserSync.stream());
});

gulp.task('clearDistCss', () => {
  gulp.src('./dist/css/*', { read: false })
    .pipe(clean());
});

gulp.task('clearDistJs', () => {
  gulp.src('./dist/js/*', { read: false })
    .pipe(clean());
});

gulp.task('css', ['clearDistCss'], () => {
  gulp.src('./src/**/*.css')
    .pipe(plumber())
    .pipe(sourcemaps.init())
    .pipe(postcss([
      postcssImport(),
      autoprefixer(),
      postcssCustomProperties(),
      postcssApply(),
      postcssCalc(),
      postcssRgba(),
      postcssPseudoelements(),
      pixrem(),
    ]))
    .pipe(sourcemaps.write())
    .pipe(rename({ dirname: '' }))
    .pipe(rev())
    .pipe(cleanCSS({ compatibility: 'ie8' }))
    .pipe(gulp.dest('./dist/css'))
    .pipe(rev.manifest())
    .pipe(gulp.dest('rev/css'))
    .pipe(browserSync.stream());
});

gulp.task('js', ['clearDistJs'], () => {
  gulp.src('./src/**/*.js')
    .pipe(plumber())
    .pipe(eslint())
    .pipe(eslint.format())
    .pipe(eslint.failOnError())
    .pipe(sourcemaps.init())
    .pipe(rollup({
      allowRealFiles: true,
      entry: [
        './src/example/example.js',
      ],
      format: 'iife',
      external: ['jquery'],
      plugins: [
        nodeResolve({
          jsnext: true,
        }),
        commonjs(),
        buble(),
      ],
    }))
    .pipe(sourcemaps.write())
    .pipe(rename({ dirname: '' }))
    .pipe(rev())
    .pipe(uglify())
    .pipe(gulp.dest('./dist/js'))
    .pipe(rev.manifest())
    .pipe(gulp.dest('rev/js'))
    .pipe(browserSync.stream());
});

gulp.task('html', () => {
  gulp.src(['rev/**/*.json', './src/**/*.html'])
    .pipe(revCollector({
      replaceReved: true,
    }))
    .pipe(plumber())
    .pipe(rename({ dirname: '' }))
    .pipe(htmlmin({ collapseWhitespace: true }))
    .pipe(gulp.dest('./dist'))
    .pipe(browserSync.stream());
});

gulp.task('server', ['html', 'image', 'css', 'js', 'lib'], () => {
  browserSync.init({
    server: {
      baseDir: ['./dist', './node_modules'],
    },
  });

  gulp.watch(['./src/**/*.html', './rev/**/*'], ['html', 'lib']);
  gulp.watch('./src/**/*.{jpg,png}', ['image']);
  gulp.watch('./src/**/*.css', ['css']);
  gulp.watch('./src/**/*.js', ['js']);
});

gulp.task('default', ['server']);
